package com.example.qixinshangwutong.view.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;

import com.example.qixinshangwutong.BaseApplication;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.model.news.FmentAdapter;
import com.example.qixinshangwutong.model.news.Responese;
import com.example.qixinshangwutong.model.news.UILunBo;
import com.example.qixinshangwutong.model.news.hot_baodao;
import com.example.qixinshangwutong.model.news.mtbd_ar_list;
import com.example.qixinshangwutong.model.news.values;
import com.example.qixinshangwutong.view.activity.NewsActivity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;



public class NewsFragment extends Fragment {
    private static final String TAG = "6666666";
    private Banner banner;
    private UILunBo uilunbo;
    private int page=1;
    private List<String> list=new ArrayList<>();
    private List<String> listbelow=new ArrayList<>();
    private List<String> listid=new ArrayList<>();
    private SwipeRecyclerView recyclerView;
    private RefreshLayout refresh;
    private List<UILunBo> thridlisttime=new ArrayList<>();
    private FmentAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root=inflater.inflate(R.layout.fragment_news, container, false);
        intiview(root);
        listload(root);
        return root;
    }

    private void intiview(View root) {
        banner = root.findViewById(R.id.banner);
        BaseApplication.api.requestWeather()
                .map(new Func1<Responese<values>, hot_baodao[]>() {
                    @Override
                    public hot_baodao[] call(Responese<values> valuesResponese) {
                        return valuesResponese.getValues().getHot_baodao();
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<hot_baodao[]>() {
                    @Override
                    public void onCompleted() {
                        banner.setImageLoader(new ImageLoader() {
                            @Override
                            public void displayImage(Context context, Object path, ImageView imageView) {
                                Glide.with(requireActivity()).load(path).into(imageView);
                            }
                        });
                        //设置轮播的动画效果,里面有很多种特效,可以到GitHub上查看文档。
                        banner.setBannerAnimation(Transformer.DepthPage);
                        banner.setImages(list);//设置图片资源
                        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);//设置banner显示样式（带标题的样式）
                        banner.setBannerTitles(listbelow); //设置标题集合（当banner样式有显示title时）
                        //设置指示器位置（即图片下面的那个小圆点）
                        banner.setIndicatorGravity(BannerConfig.CENTER);
                        banner.setDelayTime(2000);//设置轮播时间2秒切换下一图
                        banner.start();//开始进行banner渲染
                        banner.setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {
                                Intent intent = new Intent(requireActivity(), NewsActivity.class);
                                intent.putExtra("name", listbelow.get(position));
                                intent.putExtra("uri", "http://android.qxswt.com/mediadetail/" + listid.get(position));
                                startActivity(intent);

                            }
                        });
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(hot_baodao[] hot_baodaos) {
                        for (int i = 0; i < hot_baodaos.length; i++) {
                            list.add(hot_baodaos[i].getUrl());
                            listbelow.add(hot_baodaos[i].getTitle());
                            listid.add(hot_baodaos[i].getId());
                        }
                    }
                });
    }
        private void listload (View root){
            recyclerView = root.findViewById(R.id.recyclerview_linear);
            refresh = root.findViewById(R.id.refresh);
            LinearLayoutManager llm = new LinearLayoutManager(requireActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(llm);
            listview(page);
        }
        public void listview(int page){
            BaseApplication.api.thridlist(page).map(new Func1<Responese<values>, mtbd_ar_list[]>() {
                @Override
                public mtbd_ar_list[] call(Responese<values> valuesResponese) {
                    return valuesResponese.getValues().getMtbd_ar_list();
                }

            }).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Subscriber <mtbd_ar_list[]>() {
                        @Override
                        public void onCompleted() {
                            adapter=new FmentAdapter(requireActivity());
                            adapter.setData(thridlisttime);
                            recyclerView.setAdapter(adapter);
                            clickrefsh();

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(mtbd_ar_list[] mtbd_ar_lists) {
                            for(int i=0;i<mtbd_ar_lists.length;i++) {
                                UILunBo uiLunBo=new UILunBo();
                                uiLunBo.setUrl(mtbd_ar_lists[i].getUrl());
                                uiLunBo.setAdd_time(mtbd_ar_lists[i].getAdd_time());
                                uiLunBo.setTitle(mtbd_ar_lists[i].getTitle());
                                uiLunBo.setId(mtbd_ar_lists[i].getId());
                                thridlisttime.add(uiLunBo);
                            }
                        }

                    });

        }
        private void clickrefsh(){
            refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
                @Override
                public void onLoadMore(RefreshLayout refreshLayout) {
                    page++;
                    listview(page);
                    adapter.notifyDataSetChanged();
                    refresh.finishLoadMore(1000,true,false);
                }
            });
        }





    }

